package com.example.monitoringsystem.controller;

import com.example.monitoringsystem.bean.Health;
import com.example.monitoringsystem.currentUser.StaticUser;
import com.example.monitoringsystem.mapper.HealthMapper;
import com.example.monitoringsystem.mapper.PatientMapper;
import com.example.monitoringsystem.service.ManagerStatisticPatientService;
import com.example.monitoringsystem.utils.DateUtils;
import com.example.monitoringsystem.utils.HealthUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class ManagerStatisticPatientController {
    @Autowired
    private ManagerStatisticPatientService managerStatisticPatientService;

    @Autowired
    private PatientMapper patientMapper;

    // 获取当前患者的按时间排序的全部健康信息
    @RequestMapping("/admin/getHealthOfPatient")
    public List<Health> getAllHealthImformationOfPatient(@RequestParam Integer patientId){
        return managerStatisticPatientService.getAllHealthImformationOfPatient(patientId);
    }

    // 获取当前患者的按时间排序的全部健康信息
    @RequestMapping("/admin/getXAndY")
    public Map<String,List> getXAndY(@RequestParam Integer patientId){
        Map<String,List> map = new HashMap<>();
        List<Health> healths = managerStatisticPatientService.getAllHealthImformationOfPatient(patientId);
        List<String> x = new ArrayList<>();// 时间x轴
        List<Float> yT = new ArrayList<>();// 温度y轴
        List<Integer> yP = new ArrayList<>();// 心跳
        List<Integer> yB = new ArrayList<>();// 血压
        for(Health health:healths){
            x.add(DateUtils.toString(health.getDate()));
            yT.add(health.getTemperature());
            yP.add(health.getPulse());
            yB.add(health.getBloodPressure());
        }

        map.put("x",x);
        map.put("yT",yT);
        map.put("yP",yP);
        map.put("yB",yB);
        return map;
    }

    // 获取当天全部患者的健康信息
    @RequestMapping("/admin/getHealthOfToday")
    public List<Health> getAllHealthImformationOfDate(){
        return managerStatisticPatientService.getAllHealthImformationOfDate(DateUtils.getCurrentSqlDate());
    }

    @RequestMapping("/admin/getStatisis")
    public Map<String,Integer> getStatisis(){
       List<Health> healths = managerStatisticPatientService.getAllHealthImformationOfDate(DateUtils.getCurrentSqlDate());
       Map<String,Integer> map = new HashMap<>();

       int healthN = 0;
       int unhealth = 0;
       for(Health health:healths){
           if(HealthUtils.diagnosisHealth(health).length() == 0) healthN++;
           else unhealth++;
       }
       int up = healthN + unhealth;
       int all = patientMapper.selectCount(null);
       int unup = all -up;
       if (unup<0) unup =2;
       map.put("unup",unup);
       map.put("health",healthN);
       map.put("unhealth",unhealth);
       return map;
    }

}
